package com.matt.service.impl;

import com.matt.domain.Atta;
import com.matt.foundation.constant.FileConstant;
import com.matt.foundation.ext.aliyun.OSSProcessService;
import com.matt.commons.page.PagerQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.repository.AttaRepository;
import com.matt.service.AttaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Describle This Class Is
 * @Author ZengMin
 * @Date 2018/12/12 16:55
 * @Company Matt
 */
@Service
public class AttaServiceImpl implements AttaService {

    @Autowired
    AttaRepository attaRepository;

    @Autowired
    private OSSProcessService ossProcessService;

    @Autowired
    AttaService attaService;

    @Value("${aliyun.oss.filePathPrefix}")
    private String filePathPrefix;

    @Value("${aliyun.oss.fileServerPrefix}")
    private String IMG_SERVICE_PREFIX;

    @Override
    public String uploadFile(MultipartFile file, Atta atta) {
        String originalName = file.getOriginalFilename();
        String extension = "";
        String fileName = "";
        if (originalName.indexOf(".") != -1) {
            extension = originalName.substring(originalName.lastIndexOf("."));
            fileName = originalName.substring(0, originalName.indexOf("."));
        } else {
            fileName = originalName;
        }
        String filePath = atta.getCid() + "/" + DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd_HH-mm-ss-SSS") + extension;
        String relativePath = ossProcessService.aliyunOssUpload(filePathPrefix + "children/" + filePath, file);
        atta.setUrl(IMG_SERVICE_PREFIX + relativePath);
        atta.setName(fileName);
        atta.setFileType(extension);
        Integer attaType = atta.getAttaType();
        if (attaType == null) {
            attaType = 0;
            atta.setAttaType(attaType);
        }
        switch (attaType) {
            case 0:
                atta.setAttaTypeVal("普通附件");
                break;
            case 1:
                atta.setAttaTypeVal("评测附件");
                break;
            default:
                break;
        }
        attaRepository.save(atta);
        return IMG_SERVICE_PREFIX + relativePath;
    }

    @Override
    public Page getAllByCidAndType(String childrenId, Integer attaType, PagerQuery pagerQuery) {
        Pageable pageRequest = PageRequest.of(pagerQuery.getPageNum(), pagerQuery.getPageSize());
        Page allByCidAndAttaType;
        if (attaType == FileConstant.ATTATYPE_ALL) {
            allByCidAndAttaType = attaRepository.findAllByCid(childrenId, pageRequest);
        } else {
            allByCidAndAttaType = attaRepository.findAllByCidAndAttaType(childrenId, attaType, pageRequest);
        }
        return allByCidAndAttaType;
    }
}
